package com.ruoyi.web.controller.estate;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.estate.News;
import com.ruoyi.system.service.estate.INewsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资讯管理Controller
 */
@RestController
@RequestMapping("/estate/news")
public class NewsController extends BaseController {
    @Autowired
    private INewsService newsService;

    /**
     * 查询资讯列表
     */
    @PreAuthorize("@ss.hasPermi('estate:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(News news) {
        startPage();
        List<News> list = newsService.selectNewsList(news);
        return getDataTable(list);
    }

    /**
     * 导出资讯列表
     */
    @PreAuthorize("@ss.hasPermi('estate:news:export')")
    @Log(title = "资讯管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(News news) {
        List<News> list = newsService.selectNewsList(news);
        ExcelUtil<News> util = new ExcelUtil<News>(News.class);
        return util.exportExcel(list, "资讯数据");
    }

    /**
     * 获取资讯详细信息
     */
    @PreAuthorize("@ss.hasPermi('estate:news:query')")
    @GetMapping(value = "/{newsId}")
    public AjaxResult getInfo(@PathVariable("newsId") Long newsId) {
        return success(newsService.selectNewsById(newsId));
    }

    /**
     * 新增资讯
     */
    @PreAuthorize("@ss.hasPermi('estate:news:add')")
    @Log(title = "资讯管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody News news) {
        // 处理图片URL，确保格式正确
        if (news.getNewsImage() != null && news.getNewsImage().startsWith("/profile")) {
            // 如果是相对路径，不做处理，保持原样
            // 系统会根据配置自动处理URL
        }
        return toAjax(newsService.insertNews(news));
    }

    /**
     * 修改资讯
     */
    @PreAuthorize("@ss.hasPermi('estate:news:edit')")
    @Log(title = "资讯管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody News news) {
        // 处理图片URL，确保格式正确
        if (news.getNewsImage() != null && news.getNewsImage().startsWith("http://localhost:8080/profile")) {
            // 如果是完整URL，去掉前缀，只保留相对路径
            news.setNewsImage(news.getNewsImage().replace("http://localhost:8080", ""));
        }
        return toAjax(newsService.updateNews(news));
    }

    /**
     * 删除资讯
     */
    @PreAuthorize("@ss.hasPermi('estate:news:remove')")
    @Log(title = "资讯管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{newsIds}")
    public AjaxResult remove(@PathVariable Long[] newsIds) {
        return toAjax(newsService.deleteNewsByIds(newsIds));
    }
    
    /**
     * 更新资讯浏览次数
     */
    @PreAuthorize("@ss.hasPermi('estate:news:edit')")
    @Log(title = "资讯管理", businessType = BusinessType.UPDATE)
    @PutMapping("/view/{newsId}")
    public AjaxResult updateViewCount(@PathVariable("newsId") Long newsId) {
        return toAjax(newsService.updateViewCount(newsId));
    }
    
    /**
     * 发布资讯
     */
    @PreAuthorize("@ss.hasPermi('estate:news:edit')")
    @Log(title = "资讯管理", businessType = BusinessType.UPDATE)
    @PutMapping("/publish/{newsId}")
    public AjaxResult publish(@PathVariable("newsId") Long newsId) {
        return toAjax(newsService.publishNews(newsId));
    }
    
    /**
     * 下架资讯
     */
    @PreAuthorize("@ss.hasPermi('estate:news:edit')")
    @Log(title = "资讯管理", businessType = BusinessType.UPDATE)
    @PutMapping("/unpublish/{newsId}")
    public AjaxResult unpublish(@PathVariable("newsId") Long newsId) {
        return toAjax(newsService.unpublishNews(newsId));
    }
    
    /**
     * 置顶资讯
     */
    @PreAuthorize("@ss.hasPermi('estate:news:edit')")
    @Log(title = "资讯管理", businessType = BusinessType.UPDATE)
    @PutMapping("/top/{newsId}")
    public AjaxResult setTop(@PathVariable("newsId") Long newsId) {
        return toAjax(newsService.setTopNews(newsId));
    }
    
    /**
     * 取消置顶资讯
     */
    @PreAuthorize("@ss.hasPermi('estate:news:edit')")
    @Log(title = "资讯管理", businessType = BusinessType.UPDATE)
    @PutMapping("/cancelTop/{newsId}")
    public AjaxResult cancelTop(@PathVariable("newsId") Long newsId) {
        return toAjax(newsService.cancelTopNews(newsId));
    }
}
